//
//  WordDicViewController.m
//  万词王
//
//  Created by rimi on 2016/11/29.
//  Copyright © 2016年 dongfumei. All rights reserved.
//

#import "WordDicViewController.h"
#import "WordDicTableViewController.h"
#import "WordDicTableViewCell.h"

@interface WordDicViewController () <UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,UISearchResultsUpdating>

// 主页滚动视图
@property (nonatomic, strong) UIScrollView *mainScrollView;
// 下划线
@property (nonatomic, strong) UIView *lineView;
//
@property (nonatomic, strong) UITableView *tableView;
//
@property (nonatomic, assign) CGFloat lineViewStartX;
//
@property (nonatomic, strong) NSDictionary *dataSource;
// 搜索框
@property (nonatomic, strong) UISearchController *searchController;


@end

@implementation WordDicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initializeAppearence];
    [self initalizeDataSource];
    
}

-(void)initializeAppearence{
    
    [self.view addSubview:self.mainScrollView];
    self.navigationController.navigationBar.hidden = YES;
    UIButton *accurateButton = [self navigationButtonWithTitle:@"精确查找" withFrame:CGRectMake(kBaseWidth * 0.5 - 105, 20, 100/AAdaptationWidth(), 40) withTar:1011];
    accurateButton.selected = YES;
    UIButton *blearButton = [self navigationButtonWithTitle:@"模糊查找" withFrame:CGRectMake(kBaseWidth * 0.5 + 5, 20, 100/AAdaptationWidth(), 40) withTar:1012];
    [self.navigationBars addSubview:accurateButton];
    [self.navigationBars addSubview:blearButton];
    [self.navigationBars addSubview:self.lineView];
    
}

- (void)initalizeDataSource {
    
    NSString *url=@"http://api.jisuapi.com/cidian/word";
    
    NSDictionary *dic=@{@"appkey":@"8624d50a80511fb6",@"word":@"文墨"};
    AFHTTPSessionManager   *manager=[AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/html",@"text/javascript", nil];
    
    [manager GET:url parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        
            self.dataSource=[responseObject[@"result"] mutableCopy];
    
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error.localizedDescription);
        
    }];
}



#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}

-(UIButton *)navigationButtonWithTitle:(NSString *)title withFrame:(CGRect)frame withTar:(NSInteger)tag{
    
    UIButton *button = [[UIButton alloc]initWithFrame:frame];
    button.tag = tag;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:MainColor forState:UIControlStateSelected];
    [button addTarget:self action:@selector(navaButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

#pragma mark - 点击事件
// button 的点击事件
-(void)navaButtonClick:(UIButton *)sender{
    
    if (sender.tag == 1011) {
        [self.mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }else{
        [self.mainScrollView setContentOffset:CGPointMake(kBaseWidth/AAdaptationWidth(), 0) animated:YES];
    }
}

#pragma mark - UITableViewDataSource

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *rese = @"UITableViewCell";
    WordDicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rese];
    if (!cell) {
        cell = [[WordDicTableViewCell alloc]init];
    }
    cell.nameLabel.text = _dataSource[@"content"];
    
    return cell;
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offset = scrollView.contentOffset.x;
    CGFloat precent = offset/kBaseWidth;
    
    CGRect lineRect = self.lineView.frame;
    lineRect.origin.x = (kBaseWidth *0.5) - ((kBaseWidth *0.5)-100)*precent + ((kBaseWidth/2)-100);
    UIButton *accurateButton = [self.navigationBars viewWithTag:1011];
    UIButton *blearButton = [self.navigationBars viewWithTag:1012];
    if (precent == 1) {
        [UIView animateWithDuration:1 animations:^{
            accurateButton.selected = NO;
            blearButton.selected = YES;
        }];
    } else if (precent == 0){
        [UIView animateWithDuration:1 animations:^{
            accurateButton.selected = YES;
            blearButton.selected = NO;
        }];
    }
}

-(UIScrollView *)mainScrollView{
    
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc]initWithFrame:AAdaptionRect(0, 64, kBaseWidth/AAdaptationWidth(), kBaseHeight - 64)];
        _mainScrollView.pagingEnabled = YES;
        _mainScrollView.showsVerticalScrollIndicator = NO;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.contentSize = CGSizeMake(self.mainScrollView.frame.size.width*2, self.mainScrollView.frame.size.height);
        _mainScrollView.delegate = self;
        _mainScrollView.bounces = NO;
        [self.mainScrollView addSubview:self.tableView];
        
    }
    return _mainScrollView;
}

-(UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(kBaseWidth * 0.5 - 105, 61, 100, 3)];
        
        _lineView.backgroundColor = [UIColor orangeColor];
        _lineViewStartX = _lineView.bounds.size.width+(_lineView.bounds.size.width*0.5);
    }
    return _lineView;
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:AAdaptionRect(0, 0, self.mainScrollView.frame.size.width/AAdaptationWidth(), self.mainScrollView.frame.size.height) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
    }
    return _tableView;
}

-(UISearchController *)searchController{
    if (!_searchController) {
        _searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
        _searchController.searchResultsUpdater = self;
        _searchController.searchBar.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 45);
        // 关闭搜索的遮罩效果
        _searchController.dimsBackgroundDuringPresentation = NO;
    }
    return _searchController;
}

@end
